//
//  WebViewController.m
//  LazyCat
//
//  Created by jike on 15/12/28.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "WebViewController.h"
#import "classifyViewController.h"
#import "AFNetworkHandler.h"

@interface WebViewController ()<UIWebViewDelegate,UIScrollViewDelegate,UIAlertViewDelegate,backNameAndIdDelegate>
{
    NSString *_requestUUrl;
    UILabel *_tuiJianLabel;//推荐label，
    UILabel *topLable;
    UIImageView *twoImg; // 推荐图标
}
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) UIButton *cancelBtn;
@property (nonatomic,strong) UIButton *shareBtn;
@property (nonatomic, retain) UIView *navView;
@property (nonatomic, retain) UIImageView *oneImg; // 上架下架图片
@property (nonatomic) BOOL upDown; // 上架下架状态
@property (nonatomic, retain) UILabel *oneLable; // 上架lable

@end

@implementation WebViewController

-(void)backPassName:(NSString *)name nameId:(NSString *)nameID {//用于显示传递过来的数据
    self.className = name;
    self.classID = nameID;
    if (self.className.length < 1) {
        self.className = @"";
    }
    topLable.text = [NSString stringWithFormat:@"   所属分类  %@",self.className];
}
-(void)viewWillAppear:(BOOL)animated {
    NSString *url = [NSString stringWithFormat:@"%@/LCat/Product-%@?appname=lanmao&plat=ios&username=%@&iszhanggui=1&sign2=%@",BaseUrl,self.requestUrl,[AppData share].username,[Utils md5SignStr]];
    _requestUUrl = url;
    [self createWebView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createWebView];
}
#pragma mark - webView
-(void)createWebView {
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 120)];
    self.webView.delegate = self;
    self.webView.backgroundColor = BackColor;
    self.webView.scrollView.delegate = self;
    [self.webView.scrollView setBounces:YES];
    [self.webView.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_requestUUrl]]];// 加载请求
    [self.view addSubview:self.webView];
    self.navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];// 导航View
    self.navView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.navView];
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelBtn.frame = CGRectMake(5, 20, 40, 40);// 返回button
    self.cancelBtn.backgroundColor = [UIColor darkGrayColor];
    self.cancelBtn.layer.cornerRadius = 20;
    self.cancelBtn.layer.masksToBounds = YES;
    self.cancelBtn.alpha = 0.6;
    [self.cancelBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:self.cancelBtn];
    UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(18, 30, 12, 21)];
    imv.image = [UIImage imageNamed:@"lALOAy2ooyYW_22_38"];
    [self.navView addSubview:imv];
    self.shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];// 分享butto
    self.shareBtn.frame = CGRectMake(ScreenWidth-45, 20, 40, 40);
    self.shareBtn.backgroundColor = [UIColor darkGrayColor];
    self.shareBtn.layer.cornerRadius = 20;
    self.shareBtn.layer.masksToBounds = YES;
    self.shareBtn.alpha = 0.6;
    [self.shareBtn addTarget:self action:@selector(fenXiangAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:self.shareBtn];
    UIImageView *sharimg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 35, 30, 20, 20)];
    sharimg.image = [UIImage imageNamed:@"lALOAy2opCYm_38_38"];
    [self.navView addSubview:sharimg];
    [self createBottomView];
}
-(void)createBottomView{// 底部view
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 180, ScreenWidth, 180)];
    bottomView.backgroundColor = BackColor;
    [self.view addSubview:bottomView];
    topLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 1, ScreenWidth, 44)];
    topLable.backgroundColor = [UIColor whiteColor];
    topLable.font = [UIFont fontWithName:nil size:15];
    topLable.text = [NSString stringWithFormat:@"   所属分类  %@",self.className];
    [bottomView addSubview:topLable];
    topLable.userInteractionEnabled = YES;
    UITapGestureRecognizer *toplableTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topLableTapAction)];
    [topLable addGestureRecognizer:toplableTap];
    UIImageView *jtImg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 25, 15, 8, 15)];
    jtImg.image = [UIImage imageNamed:@"go_log.png"];
    [topLable addSubview:jtImg];
    UIView *minView = [[UIView alloc] initWithFrame:CGRectMake(0, 46, ScreenWidth, 90)];// 45 x 45
    minView.backgroundColor = [UIColor whiteColor];
    [bottomView addSubview:minView];
    float whith = ScreenWidth/3;
    // 上下架
    self.oneImg = [[UIImageView alloc] initWithFrame:CGRectMake(whith/2-23, 10, 45, 45)];// 下架
    self.oneImg.image = [UIImage imageNamed:@"xqxiajia.png"];
    [minView addSubview:self.oneImg];
    self.oneLable = [[UILabel alloc]initWithFrame:CGRectMake(whith/2-23, 60, 45, 20)];
    self.oneLable.textAlignment = NSTextAlignmentCenter;
    self.oneLable.font = [UIFont fontWithName:nil size:14];
    if ([_isShangJia isEqualToString:@"1"]) {
        self.oneImg.image = [UIImage imageNamed:@"xqshangjia.png"];
        self.oneLable.text = @"上架";
    }else {
        self.oneImg.image = [UIImage imageNamed:@"xqxiajia.png"];
        self.oneLable.text = @"下架";
    }
    [minView addSubview:self.oneLable];
    self.oneImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *downjia = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(downAction)];
    [self.oneImg addGestureRecognizer:downjia];
    // 推荐
    twoImg = [[UIImageView alloc] initWithFrame:CGRectMake(whith+whith/2-23, 10, 45, 45)];
    
    [minView addSubview:twoImg];
    _tuiJianLabel = [[UILabel alloc]initWithFrame:CGRectMake(whith+whith/2-23, 60, 45, 20)];
    _tuiJianLabel.textAlignment = NSTextAlignmentCenter;
    _tuiJianLabel.font = [UIFont fontWithName:nil size:14];
    if ([_isHot isEqualToString:@"1"]) {
        _tuiJianLabel.text = @"已推荐";
        twoImg.image = [UIImage imageNamed:@"tuijian1.png"];
    }else {
        _tuiJianLabel.text = @"推荐";
        twoImg.image = [UIImage imageNamed:@"xqzan.png"];
    }
    [minView addSubview:_tuiJianLabel];
    twoImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tuijian = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tuijianAction)];
    [twoImg addGestureRecognizer:tuijian];
    UIImageView *threeImg = [[UIImageView alloc] initWithFrame:CGRectMake(whith+whith+whith/2-23, 10, 45, 45)];
    threeImg.image = [UIImage imageNamed:@"xqfenxiang.png"];// 分享
    [minView addSubview:threeImg];
    UILabel *threeLable = [[UILabel alloc]initWithFrame:CGRectMake(whith+whith+whith/2-23, 60, 45, 20)];
    threeLable.textAlignment = NSTextAlignmentCenter;
    threeLable.font = [UIFont fontWithName:nil size:14];
    threeLable.text = @"分享";
    [minView addSubview:threeLable];
    threeImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *fenxiang = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fenXiangAction)];
    [threeImg addGestureRecognizer:fenxiang];
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake(ScreenWidth/2 - 60, 143, 120, 30);// 保存按钮
    saveBtn.backgroundColor = MainColor;
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];//保存事件
    saveBtn.layer.cornerRadius = 3;
    saveBtn.clipsToBounds = YES;
    [bottomView addSubview:saveBtn];
}
-(void)saveAction {//保存事件
    NSString *url = [NSString stringWithFormat:saveProductUrl,self.classID,self.requestUrl];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = result;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            [[MessShowView share] showWithMess:@"保存成功" login:YES];
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [[MessShowView share] showWithMess:@"保存失败" login:NO];
        }
    }];
}
-(void)topLableTapAction {// 跳转所有分类页面
    classifyViewController *classVC = [[classifyViewController alloc]init];
    classVC.name = self.className;
    classVC.nameId = self.classID;
    classVC.delegate = self;
    [self.navigationController pushViewController:classVC animated:YES];
}
-(void)downAction {// 上、下架
    if ([_isShangJia isEqualToString:@"0"]) { // 上架
//        UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:nil message:@"确定下架" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
//        alertV.tag = 111;
//        [alertV show];
        NSString *url = [NSString stringWithFormat:shangXiaJiaProduct,_requestUrl,@"0"];
        [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *result = [dic objectForKey:@"m"];
            if ([result isEqualToString:@"success"]) {
                self.oneImg.image = [UIImage imageNamed:@"xqshangjia.png"];
                self.oneLable.text = @"上架";
                self.upDown = YES;
                _isShangJia = @"1";
                [AppData share].isLoadd = YES;//返回刷新
                [[MessShowView share]showWithMess:@"商品下架成功" login:YES];
            }else  {
                [[MessShowView share]showWithMess:result login:NO];
            }
        }];
    }else{ // 下架
//        UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:nil message:@"确定上架" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
//        alertV.tag = 112;
//        [alertV show];
        NSString *url = [NSString stringWithFormat:shangXiaJiaProduct,_requestUrl,@"1"];
        [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *result = [dic objectForKey:@"m"];
            if ([result isEqualToString:@"success"]) {
                self.upDown = NO;
                self.oneImg.image = [UIImage imageNamed:@"xqxiajia.png"];
                self.oneLable.text = @"下架";
                _isShangJia = @"0";
                [AppData share].isLoadd = YES;//返回刷新
                [[MessShowView share]showWithMess:@"商品上架成功" login:YES];
            }else  {
                [[MessShowView share]showWithMess:result login:NO];
            }
        }];
    }
}
#pragma mark -- uialertView代理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 111) {//下架
        if (buttonIndex == 1) {
//            NSString *url = [NSString stringWithFormat:shangXiaJiaProduct,_requestUrl,@"0"];
//            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
//                NSDictionary *dic = networkRequest.responseData;
//                NSString *result = [dic objectForKey:@"m"];
//                if ([result isEqualToString:@"success"]) {
//                    [[MessShowView share]showWithMess:@"商品下架成功" login:YES];
//                    self.oneImg.image = [UIImage imageNamed:@"xqshangjia.png"];
//                    self.oneLable.text = @"上架";
//                    self.upDown = YES;
//                    _isShangJia = @"1";
//                }else  {
//                    [[MessShowView share]showWithMess:result login:NO];
//                }
//            }];
        }
    }else if (alertView.tag == 112){
        if (buttonIndex == 1) {
//            NSString *url = [NSString stringWithFormat:shangXiaJiaProduct,_requestUrl,@"1"];
//            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
//                NSDictionary *dic = networkRequest.responseData;
//                NSString *result = [dic objectForKey:@"m"];
//                if ([result isEqualToString:@"success"]) {
//                    [[MessShowView share]showWithMess:@"商品上架成功" login:YES];
//                    self.upDown = NO;
//                    self.oneImg.image = [UIImage imageNamed:@"xqxiajia.png"];
//                    self.oneLable.text = @"下架";
//                    _isShangJia = @"0";
//                }else  {
//                    [[MessShowView share]showWithMess:result login:NO];
//                }
//            }];
        }
    }else if (alertView.tag == 113){//取消推荐
        if (buttonIndex == 1) {
//            NSString *url = [NSString stringWithFormat:isRecommend,_requestUrl,@""];
//            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
//                NSDictionary *dic = networkRequest.responseData;
//                NSString *result = [dic objectForKey:@"m"];
//                if ([result isEqualToString:@"success"]) {
//                    [[MessShowView share]showWithMess:@"操作成功" login:YES];
//                    _tuiJianLabel.text = @"推荐";
//                    twoImg.image = [UIImage imageNamed:@"xqzan.png"];
//                    _isHot = @"0";
//                }else  {
//                    [[MessShowView share]showWithMess:result login:NO];
//                }
//            }];
        }
    }else if (alertView.tag == 114){//推荐
        if (buttonIndex == 1) {
//            NSString *url= [NSString stringWithFormat:isRecommend,_requestUrl,@"1"];
//            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
//                NSDictionary *dic = networkRequest.responseData;
//                NSString *result = [dic objectForKey:@"m"];
//                if ([result isEqualToString:@"success"]) {
//                    [[MessShowView share]showWithMess:@"操作成功" login:YES];
//                    _tuiJianLabel.text = @"已推荐";
//                    twoImg.image = [UIImage imageNamed:@"tuijian1.png"];
//                    _isHot = @"1";
//                }else  {
//                    [[MessShowView share]showWithMess:result login:NO];
//                }
//            }];
        }
    }
}
#pragma mark - 推荐
-(void)tuijianAction {
    NSString *message;
    if ([_isHot isEqualToString:@"1"]) {
        message = @"取消推荐";
//        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:message delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
//        alertView.tag = 113;
//        [alertView show];
        NSString *url = [NSString stringWithFormat:isRecommend,_requestUrl,@""];
        [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *result = [dic objectForKey:@"m"];
            if ([result isEqualToString:@"success"]) {
                [[MessShowView share]showWithMess:@"操作成功" login:YES];
                _tuiJianLabel.text = @"推荐";
                twoImg.image = [UIImage imageNamed:@"xqzan.png"];
                _isHot = @"0";
                [AppData share].isLoadd = YES;//返回刷新
            }else  {
                [[MessShowView share]showWithMess:result login:NO];
            }
        }];
    }else {
//        message = @"确认推荐";
//        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:message delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
//        alertView.tag = 114;
//        [alertView show];
        NSString *url= [NSString stringWithFormat:isRecommend,_requestUrl,@"1"];
        [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *result = [dic objectForKey:@"m"];
            if ([result isEqualToString:@"success"]) {
                [[MessShowView share]showWithMess:@"操作成功" login:YES];
                _tuiJianLabel.text = @"已推荐";
                twoImg.image = [UIImage imageNamed:@"tuijian1.png"];
                _isHot = @"1";
                [AppData share].isLoadd = YES;//返回刷新
            }else  {
                [[MessShowView share]showWithMess:result login:NO];
            }
        }];
    }
}

-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 分享
-(void)fenXiangAction {
    
    NSString *url = [NSString stringWithFormat:@"%@&sharedUser=%@",_requestUUrl,[AppData share].username];
    [[ShareView share] showMess:[NSString stringWithFormat:@"%@%@",self.PName,url] withImgUrl:self.PImg url:url];
//    NSString *string = self.requestUrl;
//    NSArray *array = [string componentsSeparatedByString:@"?"];
//    NSString *ur = array[0];
//    NSString *url = [NSString stringWithFormat:@"%@?sharedUser=%@",ur,[AppData share].username];
//    [[ShareView share] showMess:[NSString stringWithFormat:@"%@%@",@"名字",url] withImgUrl:@"图片url" url:url];
}
#pragma mark - 返回顶部
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint point = scrollView.contentOffset;
    if (point.y > 100) { // 搜索view背景颜色
        self.navView.backgroundColor = [UIColor colorWithRed:255/255.0 green:198/255.0 blue:0/255.0 alpha:1];
        self.navView.alpha = point.y / 150;
        self.cancelBtn.backgroundColor = [UIColor clearColor];
        self.shareBtn.backgroundColor = [UIColor clearColor];
    }else{
        self.navView.backgroundColor = [UIColor clearColor];
        self.cancelBtn.backgroundColor = [UIColor darkGrayColor];
        self.cancelBtn.alpha = 0.6;
        self.shareBtn.backgroundColor = [UIColor darkGrayColor];
        self.shareBtn.alpha = 0.6;
    }
}
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    [AppData share].isLoad = YES;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
